
# Set up the packages, options, and working directory path
pacman::p_load(tidyverse)
set.seed(42)
options(scipen=999)
if (Sys.info()[['user']] == "alal") {
  root = "/home/alal/Dropbox/1_Research/ElecAdminFunding/ElecAdminFundingReplication"
} else {
  root = "~/Dropbox/ElecAdminFunding/ElecAdminFundingReplication"
}

# Load function for making tex table rows
source(file.path(root, "code/tables_figures/prep_table_rows.R"))

# Bring in the expenditure tercile estimates
load(file.path(root, "tmp/synth_exp_terciles.RData"))

# Set up the matrices for storing output
out = matrix(nrow=6, ncol=6)

# Name the rows and columns of the output matrices
estimators = c("vs_small", "vs_mid", "vs_large", 
  "to_small", "to_mid", "to_large")
output_components = c("te estimate", "se", "num years", "treated counties", "num counties", "n")
colnames(out) = estimators
rownames(out) = output_components

# Store the estimated effects and standard errors
out[1:2,] = cbind(t1_synth_dem$summary_table[,1],
  t2_synth_dem$summary_table[,1],
  t3_synth_dem$summary_table[,1],
  t1_synth_tur$summary_table[,1],
  t2_synth_tur$summary_table[,1],
  t3_synth_tur$summary_table[,1])

# Count the number of years and units
c = 1
for(all_ests in list(t1_synth_dem, t2_synth_dem, t3_synth_dem, 
      t1_synth_tur, t2_synth_tur, t3_synth_tur)){
    est_setup = attr(all_ests$ests[[1]], "setup")
    out[3,c] = est_setup$T0 + 1
    out[4,c] = nrow(est_setup$Y) - est_setup$N0
    out[5,c] = nrow(est_setup$Y)
    out[6,c] = nrow(est_setup$Y)*ncol(est_setup$Y)
    c = c + 1
}

# Put the estimates in a tex table
cat(
"\\begin{table}[ht]",
"\\centering",
"\\caption{\\textbf{Election Administration Grants Did Not Noticeably Advantage Democrats or Increase Turnout in 2020, Grant Size Tercile.}",
"\\label{tab:tercile}}",
"\\begin{tabular}{lccc|ccc}",
"\\toprule \\toprule",
" & \\multicolumn{3}{c}{Dem Vote Share (\\%)} & \\multicolumn{3}{c}{Turnout (\\%)} \\\\",
" & Small & Medium & Large & Small & Medium & Large \\\\",
" & Grant & Grant & Grant & Grant & Grant & Grant \\\\",
"\\midrule",
paste0("Grant Recipient in 2020", prep_table_rows(out[1,]), " \\\\"),
paste0(prep_table_rows(out[2,], se=TRUE), " \\\\[2mm]"),
paste0("Num Grant Recipients ", prep_table_rows(out[4,], wholenum=TRUE), " \\\\"),
paste0("Num Counties ", prep_table_rows(out[5,], wholenum=TRUE), " \\\\"),
paste0("Observations ", prep_table_rows(out[6,], wholenum=TRUE), " \\\\"),
"\\midrule",
"County FEs & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"Year FEs & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"County Weights & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"Year Weights & Yes & Yes & Yes & Yes & Yes & Yes \\\\",
"\\bottomrule \\bottomrule",
"\\multicolumn{7}{p{0.9\\textwidth}}{\\footnotesize Standard errors estimated with 1,000 county block ",
"bootstrap samples reported in parentheses. Data is a balanced panel of counties in the 8 presidential ",
"elections from 1992 to 2020. Grant sizes are determined by tercile of grant size per voting age resident among recipients. Small is the smallest tercile, and large is the largest tercile.}",
"\\end{tabular}",
"\\end{table}",
file=paste0(root, "/output/expterc_effects_table.tex"), sep="\n")

